/**
 * FileUserPreferences.java 18 sept 2006
 * 
 * Copyright (c) 2006 Emmanuel PUYBARET / eTeks <info@eteks.com>. All Rights
 * Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.windu2b.jcaddie.io;

import java.util.prefs.*;

import org.windu2b.jcaddie.model.*;

/**
 * User preferences intialized from
 * {@link org.windu2b.jcaddie.io.DefaultUserPreferences default user preferences}
 * and stored in user preferences on local file system.
 * 
 * @author Emmanuel Puybaret, windu.2b
 */
public class FileUserPreferences extends UserPreferences
{
	/**
	 * Creates user preferences read either from user preferences in file
	 * system, or from resource files.
	 */
	public FileUserPreferences()
	{
		DefaultUserPreferences defaultPreferences = new DefaultUserPreferences();

		// Use default catalog at this time as user has no way to modified it
		setCatalog( defaultPreferences.getCatalog() );
		Preferences preferences = getPreferences();

		Unit unit = Unit.valueOf( preferences.get( "unit", defaultPreferences
		        .getUnit().toString() ) );
		setUnit( unit );
	}




	/**
	 * Writes user preferences in current user preferences in system.
	 */
	@Override
	public void write() throws RecorderException
	{
		Preferences preferences = getPreferences();
		preferences.put( "unit", getUnit().toString() );

		try
		{
			// Write preferences
			preferences.sync();
		}
		catch ( BackingStoreException ex )
		{
			throw new RecorderException( "Couldn't write preferences", ex );
		}
	}




	/**
	 * Returns Java preferences for current system user.
	 */
	private Preferences getPreferences()
	{
		return Preferences.userNodeForPackage( FileUserPreferences.class );
	}
}